﻿Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Windows.Media.Animation
Imports Microsoft.VisualStudio.Text.Editor
Imports System.ComponentModel

Namespace VsSpeakerCompanion.UI
    Partial Public Class Sidebar
        Inherits UserControl
        Friend Property IsExpanded As Boolean

        Friend Event PowerOff As EventHandler

        ''' <summary>
        ''' Creates a new instance of <see cref="Sidebar"/>
        ''' </summary>
        Public Sub New()
            InitializeComponent()

            Me.IsExpanded = True
        End Sub

        Friend Sub SetContent(ByVal uiElement As UIElement)
            Me.contentGrid.Children.Clear()
            Me.contentGrid.Children.Add(uiElement)
        End Sub

        Friend Sub SetContent(ByVal widget As IWidget)
            Me.SetContent(widget.VisualElement)
        End Sub

        ''' <summary>
        ''' Handles the MouseEnter
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnMouseEnter(ByVal e As MouseEventArgs)
            MyBase.OnMouseEnter(e)
            Mouse.OverrideCursor = Me.Cursor
            'this.Opacity = 1;
        End Sub

        ''' <summary>
        ''' Handles the MouseLeave
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnMouseLeave(ByVal e As MouseEventArgs)
            MyBase.OnMouseLeave(e)
            Mouse.OverrideCursor = Nothing
            'this.Opacity = 0.5;
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnMouseDown(ByVal e As MouseButtonEventArgs)
            e.Handled = True
        End Sub

        Private Sub expandCollapseImageButton_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
            If IsExpanded Then
                Collapse()
            Else
                Expand()
            End If
        End Sub

        Friend Sub Expand()
            If Not IsExpanded Then
                BeginStoryboard(CType(Me.FindResource("expandSidebarAnimation"), Storyboard))
                Me.expandCollapseImageButton.ToolTip = Me.FindResource("collapseSidebarText")
                Me.expandCollapseImageButton.Source = TryCast(Me.FindResource("collapseImage"), ImageSource)
                Me.sidebarBorder.CornerRadius = New CornerRadius(0)

                IsExpanded = True
            End If
        End Sub

        Friend Sub Collapse()
            If IsExpanded Then
                BeginStoryboard(CType(Me.FindResource("collapseSidebarAnimation"), Storyboard))
                Me.expandCollapseImageButton.ToolTip = Me.FindResource("expandSidebarText")
                Me.expandCollapseImageButton.Source = TryCast(Me.FindResource("expandImage"), ImageSource)
                Me.sidebarBorder.CornerRadius = New CornerRadius(0, 0, 20, 20)

                IsExpanded = False
            End If
        End Sub

        Private Sub OnCollapseSidebarAnimationCompleted(ByVal sender As Object, ByVal e As EventArgs)
            Me.sidebarBorder.Height = Me.header.ActualHeight
        End Sub

        Private Sub OnExpandSidebarAnimationCompleted(ByVal sender As Object, ByVal e As EventArgs)
            Dim storyboard = CType(Me.FindResource("expandSidebarAnimation"), Storyboard)
            Dim animation = TryCast(storyboard.Children(0), DoubleAnimationUsingKeyFrames)

            Me.sidebarBorder.Height = animation.KeyFrames(0).Value
            Me.Height = Me.sidebarBorder.Height
        End Sub

        Private Sub powerOnOffImageButton_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
            RaiseEvent PowerOff(Me, New EventArgs)
        End Sub

        Friend Sub UpdatePowerOnOffStatus(ByVal isPoweredOn As Boolean)
            Me.powerOnOffImageButton.IsEnabled = isPoweredOn

            If isPoweredOn Then
                Me.powerOnOffImageButton.Source = TryCast(Me.FindResource("powerOnImage"), ImageSource)

            Else
                Me.powerOnOffImageButton.Source = TryCast(Me.FindResource("powerOffImage"), ImageSource)
            End If
        End Sub

        Friend Sub SetHeight(ByVal value As Double)
            Dim storyboard = CType(Me.FindResource("expandSidebarAnimation"), Storyboard)
            Dim animation = TryCast(storyboard.Children(0), DoubleAnimationUsingKeyFrames)
            animation.KeyFrames(0).Value = value

            If Me.IsExpanded Then
                Me.sidebarBorder.Height = value
                Me.Height = Me.sidebarBorder.Height
            End If
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="e"></param>
        Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
            e.Handled = True
        End Sub
    End Class
End Namespace